package com.zlp.mall.modules.pms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlp.mall.common.api.Pager;
import com.zlp.mall.common.api.RespCode;
import com.zlp.mall.common.exception.CustomException;
import com.zlp.mall.common.util.BeanToUtils;
import com.zlp.mall.common.util.MethodUtil;
import com.zlp.mall.modules.pms.dto.req.ProductAttributeReq;
import com.zlp.mall.modules.pms.dto.resp.ProductAttrInfoResp;
import com.zlp.mall.modules.pms.dto.resp.ProductAttributeResp;
import com.zlp.mall.modules.pms.mapper.ProductAttributeMapper;
import com.zlp.mall.modules.pms.model.ProductAttribute;
import com.zlp.mall.modules.pms.model.ProductAttributeCategory;
import com.zlp.mall.modules.pms.service.ProductAttributeCategoryService;
import com.zlp.mall.modules.pms.service.ProductAttributeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品属性参数表 服务实现类
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-09-23
 */
@Service
@Slf4j(topic = "ProductAttributeServiceImpl")
public class ProductAttributeServiceImpl extends ServiceImpl<ProductAttributeMapper, ProductAttribute> implements ProductAttributeService {

    @Resource
    private ProductAttributeMapper productAttributeMapper;
    @Resource
    private ProductAttributeCategoryService productAttributeCategoryService;

    @Override
    public List<ProductAttrInfoResp> getProductAttrInfo(Long productCategoryId) {

        log.info("getProductAttrInfo.req productCategoryId={}", productCategoryId);
        return productAttributeMapper.getProductAttrInfo(productCategoryId);
    }

    @Override
    public List<ProductAttribute> getProductAttrByProCatIdAndType(Long productAttributeCategoryId, int type) {

        log.info("getProductAttrByProCatIdAndType.req productAttributeCategoryId={},type={}", productAttributeCategoryId, type);
        LambdaQueryWrapper<ProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductAttribute::getProductAttributeCategoryId, productAttributeCategoryId);
        if (Objects.nonNull(type)) {
            queryWrapper.eq(ProductAttribute::getType, 1);
        }
        queryWrapper.orderByAsc(ProductAttribute::getSort);
        return this.list(queryWrapper);
    }

    @Override
    public Pager<ProductAttributeResp> getList(Long cid, Integer type, Integer pageSize, Integer pageNum) {

        log.info("getList.req cid={},type={},pageSize={},pageNum={}", cid, type, pageSize, pageNum);

        IPage<ProductAttribute> pages = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductAttribute::getProductAttributeCategoryId, cid);
        if (Objects.nonNull(type)) {
            queryWrapper.eq(ProductAttribute::getType, type);
        }
        queryWrapper.orderByAsc(ProductAttribute::getSort);
        IPage<ProductAttribute> page = this.page(pages, queryWrapper);
        List<ProductAttribute> records = page.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            List<ProductAttributeResp> productAttributeResps = BeanToUtils.entityToList(records, ProductAttributeResp.class);
            return new Pager(productAttributeResps, page.getTotal(), pageSize, pageNum);
        }
        return new Pager<>();

    }

    @Override
    public List<ProductAttribute> getProductAttrByProCatId(Long productAttributeCategoryId) {

        log.info("getProductAttrByProCatId.req productAttributeCategoryId={}", productAttributeCategoryId);
        LambdaQueryWrapper<ProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductAttribute::getProductAttributeCategoryId, productAttributeCategoryId);
        return this.list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Long> ids) {

        log.info("delete.rep ids={}", ids);
        if (CollectionUtil.isEmpty(ids)) {
            return Boolean.FALSE;
        }
        // 更新商品分类属性数量
        ProductAttribute productAttribute = null;
        for (Long id : ids) {
            productAttribute = this.getById(id);
            if (Objects.nonNull(productAttribute)) break;
        }

        ProductAttributeCategory productAttributeCategory = productAttributeCategoryService.getById(productAttribute.getProductAttributeCategoryId());
        if (Objects.equals(productAttribute.getType(), 0)) {
            productAttributeCategory.setAttributeCount(productAttributeCategory.getAttributeCount() - ids.size());
        } else if (Objects.equals(productAttribute.getType(), 1)) {
            productAttributeCategory.setParamCount(productAttributeCategory.getParamCount() - ids.size());
        }
        productAttributeCategoryService.updateById(productAttributeCategory);


        int counter = productAttributeMapper.checkProductAttId(ids);
        if (counter > 0) {
            throw new CustomException(RespCode.BACK_PMS_3005, MethodUtil.getLineInfo());
        }
        return this.removeByIds(ids);
    }

    @Override
    public ProductAttributeResp getItem(Long id) {

        log.info("getItem.req id={}", id);
        ProductAttribute productAttribute = this.getById(id);
        return BeanToUtils.doToDto(productAttribute, ProductAttributeResp.class);
    }

    @Override
    public boolean update(Long id, ProductAttributeReq productAttributeReq) {

        log.info("update.req id={},productAttributeReq={}", id, JSON.toJSONString(productAttributeReq));
        ProductAttribute productAttribute = BeanToUtils.dtoToDo(productAttributeReq, ProductAttribute.class);
        productAttribute.setId(id);
        return this.updateById(productAttribute);
    }

    @Override
    public boolean create(ProductAttributeReq productAttributeReq) {

        log.info("create.req productAttributeReq={}", JSON.toJSONString(productAttributeReq));
        ProductAttribute productAttribute = BeanToUtils.dtoToDo(productAttributeReq, ProductAttribute.class);
        this.save(productAttribute);
        //新增商品属性以后需要更新商品属性分类数量
        ProductAttributeCategory productAttributeCategory = productAttributeCategoryService.getById(productAttribute.getProductAttributeCategoryId());
        if (Objects.equals(productAttribute.getType(), 0)) {
            productAttributeCategory.setAttributeCount(productAttributeCategory.getAttributeCount() + 1);
        } else if (Objects.equals(productAttribute.getType(), 1)) {
            productAttributeCategory.setParamCount(productAttributeCategory.getParamCount() + 1);
        }
        productAttributeCategoryService.updateById(productAttributeCategory);
        return Boolean.TRUE;
    }
}
